<template>
  <div class="hello">
    <input type="search" placeholder="请输入城市名或拼音" v-model="keyword">
    <div class="show" v-show="keyword" ref="row">
      <ul>
        <li v-for="(v, i) in arrdata" :key="i" @click="citys(v)">{{v}}</li>
        <li v-show="!arrdata.length">没有查询到结果</li>
      </ul>
    </div>
  </div>
</template>

<script>
import Bscroll from "better-scroll";
export default {
  name: "search",
  data() {
    return {
      keyword: "",
      arrdata: []
    };
  },
  props: ["date"],
  watch: {
    keyword(v, i) {
      this.arrdata = [];
      console.log(this.date);
      for (let key in this.date) {
        for (let str in this.date[key]) {
          if (
            this.date[key][str].name.indexOf(v) > -1 ||
            this.date[key][str].spell.indexOf(v) > -1
          ) {
            this.arrdata.push(this.date[key][str].name);
          }
        }
      }
      console.log(this.arrdata);
    }
  },
  mounted() {
    this.scroll = new Bscroll(this.$refs.row);
  },
  methods: {
    citys(name) {
      this.$store.dispatch("cityKeys", name);
      this.$router.push("/");
      // this.$store.commit('citys',name)
      // this.$store.state.city = name
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.hello {
  height: 0.6rem;
  background-color: darkcyan;
  display: flex;
  flex-direction: row;
  align-items: center;
  text-align: center；;
}
.hello input {
  width: 98%;
  margin: 0 auto;
  border-radius: 4px;
  text-align: center;
  padding: 0 10px;
  color: #666;
}
.show {
  position: absolute;
  top: 1.4rem;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  background-color: #fff;
  z-index: 1;
}
.show ul {
  display: flex;
  flex-direction: column;
}
.show ul li {
  height: 0.5rem;
  padding: 0 0 0 0.2rem;
  border: 1px solid #ccc;
  line-height: 0.5rem;
}
</style>
